﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Net;

namespace GdCmts
{
    public class DataFunc
    {
        static public readonly DataFunc Instance = new DataFunc();

        public string ReadWebUrl(string url)
        {
            try
            {
                WebRequest req = WebRequest.Create(url);
                req.Method = "GET";   //指定提交的Method，可以为POST和GET，一定要大写   
                WebResponse res = req.GetResponse();
                System.Text.Encoding resEncoding = System.Text.Encoding.GetEncoding("GB2312");//接收的编码   
                StreamReader reader = new StreamReader(res.GetResponseStream(), resEncoding);
                string html = reader.ReadToEnd();     //接收的Html   
                reader.Close();
                res.Close();
                return html;
            }
            catch(Exception e) 
            {
                return "[错误]" + e.Message;
            }
        }

        public List<TunnelInfo> FormatData(string json)
        {
            if (json == "") return null;
            List<TunnelInfo> tis = new List<TunnelInfo>();
            LitJson.JsonData data = LitJson.JsonMapper.ToObject(json);
            LitJson.JsonData tunnel = data[0];
            for (int i = 0; i < tunnel.Count; i++)
            {
                TunnelInfo ti = new TunnelInfo();
                ti.CmtsName = tunnel[i]["cmtsName"].ToString();
                ti.TunelCode = tunnel[i]["upChannelName"].ToString();
                string[] _StrArr = tunnel[i]["warnMsg"].ToString().Split(',');
                foreach (string _str in _StrArr)
                {
                    if (_str.Contains("通道SNR:")) ti.Snr = _str.Replace("通道SNR:", "");
                }
                tis.Add(ti);
            }
            return tis;
        }
    }

    public class TunnelInfo
    {
        public string TunelCode { get; set; }   //通道名
        public string CmtsName { get; set; }    //前端名
        public string Snr { get; set; }         //通道SNR
    }
}
